<?php 
require_once APPPATH.'controllers/app.php';
class Personnels extends App {
     protected $default_sex_value_text  = array('Nữ' , 'Nam');
     function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->library('grocery_CRUD');
        $this->load->model('personnel_model');
    }
    function manager(){
        $config['per_page'] = 10;
        $data['title'] = "Thiết lập ngày làm việc";
        $data['css_files'] = array();
        $data['js_files'] = array();
        $data['sb'] = $this->_menu;
        $data['content_title'] = "Danh sách nhân sự";
       
        $config['base_url']   = base_url(). "index.php/personnels/manager";
        $config['total_rows'] = $this->personnel_model->count_all();
        //$config['per_page'] = 1;
        
        $config['first_link'] = '« Đầu';
        $config['last_link'] = 'Cuối »';
        $config['next_link'] = 'Tiếp »';
        $config['prev_link'] = '« Trước';
        $config['cur_tag_open'] = "<a   href='#' style='margin-left:4px' class='select'>";
        $config['cur_tag_close'] = '</a>';
        $start = $this->uri->segment(3);
        $this->load->library("pagination",$config);
        $data['pagination'] = $this->pagination->create_links();
        $data['info'] = $this->personnel_model->listall($config['per_page'],$start);
        $this->load->view("personnel/personnel_view",$data);    
    }
    /**
    function index(){
        $crud = new grocery_CRUD();
        $crud->set_subject('Nhân sự');
        $crud->set_table('tblpersonnels');
        $crud->columns('name','username','email','address','birthday', 'salary','otsalary' );
        $crud->edit_fields('name','address','birthday','sex','skill','calenderid', 'salary','otsalary','timeunitid','rule');
        $crud->add_fields('name','username','password','email','address','birthday','sex','skill','calenderid', 'salary','otsalary','timeunitid','rule');
        $crud->field_type('sex','true_false');
        $crud->field_type('sex','dropdown',array("2"=>'Nữ','1'=>'Nam'));
        $crud->field_type('rule','dropdown',array('1'=>'Quyền quản trị','2'=>'Quyền nhân viên'));
        $crud->set_relation('calenderid','tblcalenders','name');
        $crud->set_relation('timeunitid','tbltimeunit','name');
        $crud->display_as(array(
            'name'=>'Họ tên',
            'username'=>'Tên đăng nhập',
            'password'=>'Mật khẩu',
            'repass'=>'Nhập lại mật khẩu',
            'address'=>'Địa chỉ',
            'birthday'=>'Ngày sinh',
            'sex'=>'Giới tính',
            'skill'=>'Kĩ năng',
            'calenderid'=>'Lịch làm việc',
            'salary'=>'Lương cơ bản',
            'otsalary'=>'Lương OT',
            'timeunitid'=>'Đơn vị thời gian',
            'rule'=>'Quyền',
            'email'=>'Thư điện tử'));
        //$crud->set_rules($conf);
        //$crud->set_rules('username','Tên đăng nhập','is_unique[tblpersonnels.username]');
        //$crud->set_rules('password','Mật khẩu','callback_comparepass');
        //$crud->callback_before_update(array($this,'username_check'));
        //$crud->callback_before_insert(array($this,'repass_field_add_callback()'));
        $output = $crud->render();
        $output->title = "Nhân sự";
        $output->content_title = "Quản ký nhân sự";
        $output->sb = $this->_menu;
        $this->load->view('common/layout_view',$output);
    }
     *
     */
    function repass_field_add_callback(){
         return '<input type="password" maxlength="50" value="" name="repass">';
    }
    function comparepass($arr){
        redirect($arr);
        if($arr['password']!=$arr['repass']){
            $this->form_validation->set_message('comparepass', 'Mat khau');
            return FALSE;
        }
    }
    function search(){
        $text = $this->input->get('search_text');
        $type = $this->input->get('search_type');
        $data['title'] = "Kết quả tìm kiếm cho $text";
        $data['css_files'] = array();
        $data['js_files'] = array();
        $data['sb'] = $this->_menu;
        $data['content_title'] = "Kết quả tìm kiếm cho $text";
        $data['info'] = $this->personnel_model->search($text,$type);
        $this->load->view("personnel/personnel_search_view",$data);    
    }
    function edit($id){
       $this->loadedit($id, null);  
    }
    function editprocess($id){
         $this -> load -> library( 'form_validation' );
         $this -> form_validation -> set_error_delimiters("<div class='notification error'><div class='messages'>","<div class='close'><img alt='close' src='".base_url()."assets/public/img/icon/close.png'></div></div> </div>");
         $this -> form_validation -> set_rules( 'name', 'Tên', 'trim|required|min_length[3]|max_length[25]' );
         $this -> form_validation -> set_rules( 'email', 'Email', 'trim|valid_email|callback_checkemail_update');
         $this -> form_validation -> set_rules( 'password', 'Mật khẩu', 'trim|min_length[5]|max_length[25]|callback_checkpass' );
         $this->form_validation->set_message('checkemail_update','Email đã tồn tại');
         $this->form_validation->set_message('checkpass','Xác nhận mật khẩu chưa khớp');
         if($this->form_validation->run()==FALSE){
              $this->loadedit($id,null);
         }else {
            $arr = array(
                    'name'=>$this->input->post('name'),
                    'email'=>$this->input->post('email'),
                    'address'=>$this->input->post('address'),
                    'birthday'=>  datemysql($this->input->post('birthday')),
                    'sex'=>$this->input->post('sex'),
                    'skill'=>$this->input->post('skill'),
                    'calenderid'=>$this->input->post('calenderid'),
                    'salary'=>$this->input->post('salary'),
                    'otsalary'=>$this->input->post('otsalary'),
                    'timeunitid'=>$this->input->post('timeunitid'),
                    'rule'=>$this->input->post('rule')
                );
                $pass = $this->input->post('password');
                if($pass!=""){
                    $arr['password'] = $this->input->post('password');
                }
                $this->db->where('id',$id);
                $this->db->update('tblpersonnels',$arr);
                $this->loadedit($id,"Cập nhật thành công");
         }
    }
    function loadedit($id,$_suc){
        $data['suc'] = $_suc;
        $data['title'] = "Kết quả tìm kiếm cho";
        $data['js_files'] = array(
            base_url().'assets/public/js/jquery.ui.core.js',
            base_url().'assets/public/js/jquery.ui.widget.js',
            base_url().'assets/public/js/jquery.ui.datepicker.js',
        );
        $data['css_files'] = array(
            base_url().'assets/public/css/jquery.ui.all.css',
        );
        $data['sb'] = $this->_menu;
        $data['content_title'] = "Kết quả tìm kiếm cho ";
        $data['cur'] = $this->personnel_model->get($id);
        $this->load->model('calender_model');
        $this->load->model('timeunit_model');
        $data['cals'] = $this->calender_model->get_cal_works();
        $data['timeunits'] = $this->timeunit_model->getall();   
        $this->load->view("personnel/personnel_edit_view",$data);
    }
    function add(){
        $this->loadadd(null);
    }
    
    function loadadd($_suc=null){
        $data['suc'] = $_suc;
        $data['title'] = "Thêm nhân sự";
        $data['js_files'] = array(
            base_url().'assets/public/js/jquery.ui.core.js',
            base_url().'assets/public/js/jquery.ui.widget.js',
            base_url().'assets/public/js/jquery.ui.datepicker.js',
        );
        $data['css_files'] = array(
            base_url().'assets/public/css/jquery.ui.all.css',
        );
        $data['sb'] = $this->_menu;
        $data['content_title'] = "Thêm nhân sự ";
        $this->load->model('calender_model');
        $this->load->model('timeunit_model');
        $data['cals'] = $this->calender_model->get_cal_works();
        $data['timeunits'] = $this->timeunit_model->getall();
        $this->load->view("personnel/personnel_add_view",$data);
    }
    function addprocess(){
         $this -> load -> library( 'form_validation' );
         $this -> form_validation -> set_error_delimiters("<div class='notification error'><div class='messages'>","<div class='close'><img alt='close' src='".base_url()."assets/public/img/icon/close.png'></div></div> </div>");
         $this -> form_validation -> set_rules( 'name', 'Tên', 'trim|required|min_length[3]|max_length[25]' );
         $this -> form_validation -> set_rules( 'email', 'Email', 'trim|valid_email|callback_checkemail');
         $this -> form_validation -> set_rules( 'password', 'Mật khẩu', 'trim|required|min_length[5]|max_length[25]|callback_checkpass' );
         $this->form_validation->set_message('checkemail','Email đã tồn tại');
         $this->form_validation->set_message('checkpass','Xác nhận mật khẩu chưa khớp');
         if($this->form_validation->run()==FALSE){
              $data['suc'] = null;
              $this->loadadd();
         }else {
             $arr = array(
                'name'=>$this->input->post('name'),
                'email'=>$this->input->post('email'),
                'username'=>$this->input->post('username'),
                'password'=>$this->input->post('password'),
                'address'=>$this->input->post('address'),
                'birthday'=>  datemysql($this->input->post('birthday')),
                'sex'=>$this->input->post('sex'),
                'skill'=>$this->input->post('skill'),
                'calenderid'=>$this->input->post('calenderid'),
                'salary'=>$this->input->post('salary'),
                'otsalary'=>$this->input->post('otsalary'),
                'timeunitid'=>$this->input->post('timeunitid'),
                'rule'=>$this->input->post('rule')
            );
             $this->db->insert('tblpersonnels',$arr);
             $this->loadadd('Thêm thành công');
         }
         
    }
    function delete($id){
        $this->personnel_model->delete($id);
        redirect($_SERVER['HTTP_REFERER']);
    }
    function checkemail($_e){
        return $this->personnel_model->checkemail($_e);
    }
    function checkemail_update($_e){
        $id = $this->uri->segment(3);
        return $this->personnel_model->checkemail_update($id,$_e);
    }
    function checkpass($_p){
        $repass = $this->input->post('repass');
        if(trim($_p)==$repass) return true;
        else return false;
    }
    function logout(){
        $this->load->helper('cookie');
        $arr = array('id'=>'','name'=>'','rule'=>'');
        $this->session->unset_userdata($arr);
        delete_cookie("mycookies");
        $past = time() - 3600;
        foreach ( $_COOKIE as $key => $value )
        {
            setcookie( $key, $value, $past, '/' );
        }
        redirect(base_url().'login');
    }
   
}
?>
